var data = require("./data.js")
var mapjs = require("./map.js");
var clickjs = require("./click.js")
var mousejs = require("./mouse.js");
var catjs = require("./cat.js");
var blockjs = require("./block.js");
var trapjs = require("./trap.js");
var bombjs = require("./boom.js");
var gasjs = require("./gas.js")

var walls = [];
var urls = [
  '/images/black.png',//
  '/images/wall.png',//

  '/images/mU.png',//
  '/images/mD.png',//
  '/images/mL.png',//
  '/images/mR.png',//

  '/images/catU1.png',//
  '/images/catD1.png',//
  '/images/catL1.png',//
  '/images/catR1.png',//

  '/images/mouse.png',//

  '/images/gas.png',//
  '/images/trap.png',//
  '/images/bomb.png',//
  '/images/bombflash.png',//
  '/images/block.png',//
  '/images/light.png',//
];

function changeMouseType(type) {
  if (type == "first") {
    urls[2] = '/images/mU.png';
    urls[3] = '/images/mD.png';
    urls[4] = '/images/mL.png';
    urls[5] = '/images/mR.png';
  }
  else {
    urls[2] = '/images/mU2.png';
    urls[3] = '/images/mD2.png';
    urls[4] = '/images/mL2.png';
    urls[5] = '/images/mR2.png';
  }
}

function changeCatType(type) {
  if (type == "first") {
    urls[6] = '/images/catU1.png';
    urls[7] = '/images/catD1.png';
    urls[8] = '/images/catL1.png';
    urls[9] = '/images/catR1.png';
  }
  else {
    urls[6] = '/images/catU2.png';
    urls[7] = '/images/catD2.png';
    urls[8] = '/images/catL2.png';
    urls[9] = '/images/catR2.png';
  }
}


var picture = [];

function initMap1(map, mapW, mapH, width, mapMouse) {
  
  mapjs.initMap(map, mapW, mapH, width, mapMouse);
  for (var i = 0; i < mapH; i++) {
    for (var j = 0; j < mapW; j++) {
      var temp = {};
      temp.x = j * width / 2;
      temp.y = i * width / 2;
      temp.url = urls[map[i][j]];
      temp.flag = map[i][j];
      picture.push(temp);
    }
  }
}

function initMap(map, mapW, mapH, width, mapMouse) {
  walls=[];
  mapjs.initMap(map, mapW, mapH, width, mapMouse);
  for (var i = 0; i < mapH; i++) {
    for (var j = 0; j < mapW; j++) {
      if(data.isWall(map[i][j]))
      {
        var temp = {};
        temp.x = j * width / 2;
        temp.y = i * width / 2;
        temp.url = urls[map[i][j]];
        temp.flag = 1;
        walls.push(temp);
      }
    }
  }
}


function showMap(map, mapW, mapH, width, mapMouse)
{
  let mouses=mousejs.getInfo();
  let cats= catjs.getInfo();
  let click = clickjs.getInfo();
  let blocks = blockjs.getInfo();
  let traps = trapjs.getInfo();
  let bombs = bombjs.getInfo(map);
  let gas = gasjs.getInfo(map);

  picture = [];
  for(var i=0;i<walls.length;i++)
    picture.push(walls[i]);
  
  if(click.x!=-1)
  {
    click.url = urls[data.define.InputPoint];
    var x= click.x;
    var y=click.y;
    click.x = y * width / 2;
    click.y = x * width / 2;
    picture.push(click);
  }

  for(var i=0;i<mouses.length;i++)
  {
    var temp = {};
    temp.x = mouses[i].y * width / 2 + mouses[i].midY;
    temp.y = mouses[i].x * width / 2 + mouses[i].midX;
    temp.url = urls[map[mouses[i].x][mouses[i].y]];
    temp.flag = 1;
    picture.push(temp);
  }

  for (var i = 0; i < cats.length; i++) {
    var temp = {};
    temp.x = cats[i].y * width / 2;
    temp.y = cats[i].x * width / 2;
    temp.url = urls[map[cats[i].x][cats[i].y]];
    temp.flag = 1;
    picture.push(temp);
  }

  for (var i = 0; i < traps.length; i++) {
    var temp = {};
    temp.x = traps[i].y * width / 2;
    temp.y = traps[i].x * width / 2;
    temp.url = urls[map[traps[i].x][traps[i].y]];
    temp.flag = 1;
    picture.push(temp);
  }

  for (var i = 0; i < blocks.length; i++) {
    var temp = {};
    temp.x = blocks[i].y * width / 2;
    temp.y = blocks[i].x * width / 2;
    temp.url = urls[map[blocks[i].x][blocks[i].y]];
    temp.flag = 1;
    picture.push(temp);
  }

  for (var i = 0; i < bombs.length; i++) {
    var temp = {};
    temp.x = bombs[i].y * width / 2;
    temp.y = bombs[i].x * width / 2;
    temp.url = urls[map[bombs[i].x][bombs[i].y]];
    temp.flag = 1;
    picture.push(temp);
  }

  for (var i = 0; i < gas.length; i++) {
    var temp = {};
    temp.x = gas[i].y * width / 2;
    temp.y = gas[i].x * width / 2;
    temp.url = urls[map[gas[i].x][gas[i].y]];
    temp.flag = 1;
    picture.push(temp);
  }

  return picture;
}


function showMap1(map, mapW, mapH, width, mapMouse) {
  for (var i = 0; i < mapH; i++) {
    for (var j = 0; j < mapW; j++) {
      var t = i * mapW + j;
      picture[t].url = urls[map[i][j]];
      picture[t].flag = map[i][j];
    }
  }
  return picture;
}

function clear() {
  picture = [];
}

module.exports = {
  initMap: initMap,
  showMap: showMap,
  changeCatType: changeCatType,
  changeMouseType: changeMouseType,
  clear: clear
}